<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="Bookmark" href="/favicon.ico">
    <link rel="Shortcut Icon" href="/favicon.ico"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>
    <link rel="stylesheet" href="lib/select2/select2.min.css">

    <title>编辑任务</title>
</head>
<body>
<article class="page-container">
    <form class="form form-horizontal" id="form-admin-role-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>任务：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box">
                    <select class="select" size="1" name="customId" id="customId" disabled>
                        <option value="" selected>--选择任务--</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>任务组：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="任务组" id="jobGroup" name="jobGroup" disabled>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>CronExpression：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="Cron表达式" id="cronExp" name="cronExp">
            </div>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="button" id="submit" class="btn btn-success radius" id="admin-role-save"
                        name="admin-role-save"
                ><i class="icon-ok"></i> 提交
                </button>
                <button type="button" class="btn btn-success radius" id="admin-role-close" name="admin-role-close"
                        onclick="layer_close()"><i class="icon-ok"></i> 关闭
                </button>
                <!--<button class="btn" data-dismiss="modal" aria-hidden="true" onclick="layer_close()">关闭</button>-->
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="static/hry-auto/util.js"></script>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="lib/select2/select2.full.js"></script>
<script type="text/javascript" src="lib/select2/pinyin.js"></script>

<script type="text/javascript">
    //loading
    var layerIndex;
    $(function () {
        $.ajaxSetup({
            layerIndex: -1,
            beforeSend: function () { //插件加载前
                layerIndex = layer.load(1, {shade: [1.01, '#fff']});
            },
            error: function () { //报错时执行
                layer.alert('显示异常，请刷新后重试', {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            }
        });
    });

    $(document).ready(function () {
        //初始化定制数据源
        custom_selectEle_init("customId");
        var jobName = getParameter("jobName")
        var jobGroup = getParameter("jobGroup");
        var hryScheduler = getJob(jobName, jobGroup);
        console.log(hryScheduler);
        $("#customId").val(hryScheduler.trigger_name).trigger("change");
        $("#jobGroup").val(hryScheduler.trigger_group);
        $("#cronExp").val(hryScheduler.cron_expression);
        layer.close(layerIndex);
    });

    //更新Job
    $("#submit").click(function () {
        var customId = $("#customId").val();
        var group = $("#jobGroup").val();
        var cronExp = $("#cronExp").val();
        if (customId.trim == "" || group.trim() == "" || cronExp.trim() == "") {
            layer.msg("任务,任务组,CronExpression必填!");
            return;
        }
        layerIndex = layer.load(1, {shade: [0.01, '#fff']});
        var selectedText = $("#customId").find("option:selected").text();//类似:(127_鲁奇伟_CS1)鲁奇伟的定制测试-Scheduler专用定制

        /*
        var text = selectedText.substring(selectedText.indexOf("(") + 1, selectedText.indexOf(")"))//127_鲁奇伟_CS1
        var strings = text.split("_");
        var customUserName = strings[1];
        var env = strings[2];
        */

        var jobDataMap = {};
        jobDataMap.customId = customId;
        jobDataMap.executeUserId = $.cookie("uidCookie");
        jobDataMap.executeUserName = $.cookie("realnameCookie");
        var param = {};
        param.jobName = customId;
        param.jobGroup = group;
        param.desc = selectedText;
        param.cronExp = cronExp;
        param.jobDataMap = jobDataMap;

        $.ajax({
            type: "post",
            url: "/scheduler/updateJob",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(param),
            dataType: "json",
            success: function (res) {
                layer.close(layerIndex);
                if (res.status == 0) {//添加成功
                    parent.window.location.reload();
                } else {
                    layer.alert(res.msg, {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                }
            },
            fail: function (res) {
                layer.close(layerIndex);
                layer.alert(JSON.stringify(res), {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            },
            error: function (xhr) {
                layer.close(layerIndex);
                layer.alert('Error' + JSON.stringify(xhr), {
                    icon: 2,
                    skin: 'layer-ext-moon'
                });
            }
        });
    });

    function getJob(jobName, jobGroup) {
        var ret = null;
        var param = {};
        param.jobName = jobName;
        param.jobGroup = jobGroup;
        $.ajax({
            type: "post",
            url: "/scheduler/selectOneJob",
            async: false,
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(param),
            dataType: "json",
            success: function (res) {
                layer.close(layerIndex);
                if (res.status == 0) {//添加成功
                    ret = res.data;
                } else {
                    layer.alert(res.msg, {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                }
            },
            fail: function (res) {
                layer.close(layerIndex);
                layer.alert(JSON.stringify(res), {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            },
            error: function (xhr) {
                layer.close(layerIndex);
                layer.alert('Error' + JSON.stringify(xhr), {
                    icon: 2,
                    skin: 'layer-ext-moon'
                });
            }
        });
        return ret;
    }

    /*用例-编辑*/
    function tcase_edit(title, url, id, skip) {
        url = url + "?id=" + id + "&skip=" + skip;
        layer_show(title, url);
    }

    function clearAll(obj, serviceId) {
        if (obj.checked == false) {
            $("#service-" + serviceId + " :checkbox").prop("checked", false);
        }
    }

    //添加定制测试
    function addCustom() {
        var customname = $("#customname").val();
        var env = $("#env").val();
        var envKey = $("#env").find("option:selected").text();
        var strategy = $("#strategy").val();
        var userid = $.cookie('uidCookie');
        var userName = $.cookie("realnameCookie");
        var intersect = $("#intersect").val();

        var service = new Array();
        $("#services :checkbox:checked").each(function (i) {
            var value = $(this).val();
            var name = $(this).attr("name");
            if (name.match("service")) {
                var tClass = $("#tClass-" + value + "").val();
                if (tClass == "请选择测试类") {
                    tClass = "";
                }
                var s = new Object();
                s.clientlevel = 1;
                s.clientid = value;
                /*s.clientname=getServiceKey(value);*/
                var ps = $("#ps_" + value).val();
                s.priority = ps;
                var innerText = document.getElementById("serviceBox-" + value).parentNode.innerText;
                s.clientname = innerText;
                s.classname = tClass;
                s.parentclientid = 0;
                var obj = $("#service-" + value + " :checkbox:checked");
                if (obj.length > 1) {
                    s.haschild = true;
                } else {
                    s.haschild = false;
                }
                service[i] = s;

            } else if (name.match("ti")) {
                //var ti=getTiById(value);
                var s = new Object();
                s.clientlevel = 2;
                s.clientid = value;
                // s.clientname=ti.iuri;
                // s.parentclientid=ti.serviceid;
                var pi = $("#pi_" + value).val();
                s.priority = pi;
                var tiLabel = document.getElementById("tiBox-" + value);
                var tiInnerText = tiLabel.parentNode.innerText;
                var clientName = tiInnerText.substring(0, tiInnerText.indexOf("("));
                s.clientname = clientName;

                var tiDl = document.getElementById("ti-" + value);
                var tserviceIDAttr = tiDl.parentNode.parentNode.getAttribute("id");
                var sid = tserviceIDAttr.substring(tserviceIDAttr.indexOf("-") + 1);
                s.parentclientid = sid;

                var obj = $("#ti-" + value + " :checkbox:checked");
                if (obj.length > 1) {
                    s.haschild = true;
                } else {
                    s.haschild = false;
                }
                service[i] = s;

            } else if (name.match("tcase")) {
                // var tcase=getTcaseById(value);
                var s = new Object();
                s.clientlevel = 3;
                s.clientid = value;
                // s.clientname=tcase.casename;
                // s.parentclientid=tcase.iid;
                var pc = $("#pc_" + value).val();
                s.priority = pc;
                var tcaseLabel = document.getElementById("caseBox-" + value);
                var tcaseInnerText = tcaseLabel.parentNode.innerText;
                var tcaseclientname = tcaseInnerText.substring(0, tcaseInnerText.indexOf("("));
                s.clientname = tcaseclientname;
                var tcaseDl = document.getElementById("case-" + value);
                var tiIDAttr = tcaseDl.parentNode.parentNode.getAttribute("id");
                var iid = tiIDAttr.substring(tiIDAttr.indexOf("-") + 1);
                s.parentclientid = iid;
                s.haschild = false;
                service[i] = s;
            }
        });

        if (customname == null || customname == "") {
            layer.msg("定制名称不能为空！");
            return;
        } else if (service == null || service == "") {
            layer.msg("请选择服务！");
            return;
        } else if (env == null || env == "") {
            layer.msg("请选择环境！");
            return;
        } else {
            layerIndex = layer.load(1, {shade: [0.01, '#fff']});
            var obj = new Object();
            obj.customname = customname;
            obj.userid = userid;
            obj.username = userName;
            obj.envid = env;
            obj.envkey = envKey;
            obj.strategy = strategy;
            obj.intersect = intersect;
            obj.tcustomdetails = service;
            var json = JSON.stringify(obj);
            console.log(json);

            $.ajax({
                type: "post",
                url: "/tcustom/insertOne",
                data: json,
                dataType: "json",
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    var status = data.status;
                    var msg = data.msg;
                    if (status == 0) {
                        layer.close(layerIndex);
                        parent.window.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else {
                        layer.close(layerIndex);
                        layer.alert(msg, {
                            icon: 0,
                            skin: 'layer-ext-moon'
                        })
                    }

                },
                fail: function (data) {
                    layer.close(layerIndex);
                    layer.alert(JSON.stringify(data), {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    })
                },
                error: function (xhr) {
                    layer.close(layerIndex);
                    layer.alert('Error' + JSON.stringify(xhr), {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    })
                }
            });
        }

    }


</script>

</body>
</html>